import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import TaskModel 1.0

GroupBox {
    title: "任务管理"

    property TaskModel taskModel
    property var dayPointsArray: [10, 10, 10, 10, 10, 10, 10]

    ColumnLayout {
        width: parent.width
        spacing: 10

        // 任务添加部分
        GroupBox {
            title: "添加新任务"
            Layout.fillWidth: true

            GridLayout {
                columns: 2
                width: parent.width

                Label { text: "任务名称:" }
                TextField {
                    id: taskNameField
                    Layout.fillWidth: true
                    placeholderText: "输入任务名称"
                }

                Label { text: "执行次数:" }
                SpinBox {
                    id: executeCountField
                    Layout.fillWidth: true
                    from: 0
                    to: 1000
                    value: 1
                }

                Label { text: "周积分设置 (7天):" }
                RowLayout {
                    id: dayPointsRow
                    Layout.fillWidth: true
                    Repeater {
                        model: 7
                        SpinBox {
                            Layout.fillWidth: true
                            from: 0
                            to: 100
                            value: dayPointsArray[index]
                            onValueChanged: dayPointsArray[index] = value
                            ToolTip.text: "第 " + (index + 1) + " 天积分"
                        }
                    }
                }

                Item { Layout.fillWidth: true; Layout.columnSpan: 1 }
                Button {
                    text: "添加任务"
                    Layout.fillWidth: true
                    Layout.columnSpan: 1
                    onClicked: {
                        if (taskNameField.text.trim() === "") {
                            taskMessage.text = "错误：任务名称不能为空！"
                            taskMessage.color = "red"
                            taskMessage.visible = true
                            taskMessageTimer.restart()
                            return
                        }

                        var points = [];
                        for (var i = 0; i < 7; i++) {
                            points.push(dayPointsArray[i]);
                        }

                        taskModel.addTask(taskNameField.text, executeCountField.value, points);

                        taskMessage.text = "任务添加成功！"
                        taskMessage.color = "green"
                        taskMessage.visible = true
                        taskMessageTimer.restart()

                        // 清空输入
                        taskNameField.text = ""
                        executeCountField.value = 1
                        // 重置积分数组为默认值
                        for (var j = 0; j < 7; j++) {
                            dayPointsArray[j] = 10;
                        }
                    }
                }
            }
        }

        // 任务列表
        GroupBox {
            title: "任务列表"
            Layout.fillWidth: true

            ColumnLayout {
                width: parent.width
                spacing: 5
                ListView {
                    id: taskListView
                    Layout.fillWidth: true
                    Layout.preferredHeight: 300
                    model: taskModel
                    delegate: TaskItemDelegate {
                        kidTaskModel: taskModel
                        // 移除 taskModel: taskModel 这一行
                        onTaskCompleted:(points)=> {
                            taskMessage.text = "任务完成！获得 " + points + " 积分"
                            taskMessage.color = "green"
                            taskMessage.visible = true
                            taskMessageTimer.restart()
                        }
                    }
                }

                // 任务操作按钮
                RowLayout {
                    Layout.fillWidth: true
                    spacing: 10

                    Button {
                        text: "保存任务数据"
                        Layout.fillWidth: true
                        onClicked: {
                            taskModel.saveTaskData()
                            taskMessage.text = "任务数据保存成功！"
                            taskMessage.color = "green"
                            taskMessage.visible = true
                            taskMessageTimer.restart()
                        }
                    }

                    Button {
                        text: "加载任务数据"
                        Layout.fillWidth: true
                        onClicked: {
                            taskModel.loadTaskData()
                            taskMessage.text = "任务数据加载成功！"
                            taskMessage.color = "blue"
                            taskMessage.visible = true
                            taskMessageTimer.restart()
                        }
                    }

                    Button {
                        text: "清空所有任务"
                        Layout.fillWidth: true
                        onClicked: {
                            taskModel.clearAll()
                            taskMessage.text = "所有任务已清空！"
                            taskMessage.color = "orange"
                            taskMessage.visible = true
                            taskMessageTimer.restart()
                        }
                    }

                    Button {
                        text: "添加示例任务"
                        Layout.fillWidth: true
                        onClicked: {
                            var points = [5, 5, 10, 10, 15, 15, 20];
                            taskModel.addTask("示例任务 " + (taskModel.rowCount() + 1),
                                             Math.floor(Math.random() * 10) + 1,
                                             points);
                            taskMessage.text = "示例任务添加成功！"
                            taskMessage.color = "green"
                            taskMessage.visible = true
                            taskMessageTimer.restart()
                        }
                    }
                }
            }
        }

        // 任务操作消息
        Label {
            id: taskMessage
            Layout.fillWidth: true
            visible: false
            horizontalAlignment: Text.AlignHCenter
            wrapMode: Text.Wrap
        }
    }

    Timer {
        id: taskMessageTimer
        interval: 3000
        onTriggered: taskMessage.visible = false
    }
}
